home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / dflat8.zip / WINDOW.C < prev   
Text File  |  1991-09-30  |  15KB  |  493 lines

  1. /* ---------- window.c ------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. WINDOW inFocus = NULL;
  6.  
  7. int foreground, background;   /* current video colors */
  8.  
  9. static void TopLine(WINDOW, int, RECT);
  10.  
  11. /* --------- create a window ------------ */
  12. WINDOW CreateWindow(
  13.     CLASS class,              /* class of this window       */
  14.     char *ttl,                /* title or NULL              */
  15.     int left, int top,        /* upper left coordinates     */
  16.     int height, int width,    /* dimensions                 */
  17.     void *extension,          /* pointer to additional data */
  18.     WINDOW parent,            /* parent of this window      */
  19.     int (*wndproc)(struct window *,enum messages,PARAM,PARAM),
  20.     int attrib)               /* window attribute           */
  21. {
  22.     WINDOW wnd = calloc(1, sizeof(struct window));
  23.     get_videomode();
  24.     if (wnd != NULL)    {
  25.         int base;
  26.         /* ----- height, width = -1: fill the screen ------- */
  27.         if (height == -1)
  28.             height = SCREENHEIGHT;
  29.         if (width == -1)
  30.             width = SCREENWIDTH;
  31.         /* ----- coordinates -1, -1 = center the window ---- */
  32.         if (left == -1)
  33.             wnd->rc.lf = (SCREENWIDTH-width)/2;
  34.         else
  35.             wnd->rc.lf = left;
  36.         if (top == -1)
  37.             wnd->rc.tp = (SCREENHEIGHT-height)/2;
  38.         else
  39.             wnd->rc.tp = top;
  40.         wnd->attrib = attrib;
  41.         if (ttl != NULL)
  42.             AddAttribute(wnd, HASTITLEBAR);
  43.         if (wndproc == NULL)
  44.             wnd->wndproc = classdefs[class].wndproc;
  45.         else
  46.             wnd->wndproc = wndproc;
  47.         /* ---- derive attributes of base classes ---- */
  48.         base = class;
  49.         while (base != -1)    {
  50.             AddAttribute(wnd, classdefs[base].attrib);
  51.             base = classdefs[base].base;
  52.         }
  53.         if (parent && !TestAttribute(wnd, NOCLIP))    {
  54.             /* -- keep upper left within borders of parent - */
  55.             wnd->rc.lf = max(wnd->rc.lf,GetClientLeft(parent));
  56.             wnd->rc.tp = max(wnd->rc.tp,GetClientTop(parent));
  57.         }
  58.         wnd->class = class;
  59.         wnd->extension = extension;
  60.         wnd->rc.rt = GetLeft(wnd)+width-1;
  61.         wnd->rc.bt = GetTop(wnd)+height-1;
  62.         wnd->ht = height;
  63.         wnd->wd = width;
  64.         if (ttl != NULL)
  65.             InsertTitle(wnd, ttl);
  66.         wnd->nextfocus = wnd->prevfocus = wnd->dFocus = NULL;
  67.         wnd->parent = parent;
  68.         wnd->oldcondition = wnd->condition = ISRESTORED;
  69.         wnd->RestoredRC = wnd->rc;
  70.         wnd->PrevKeyboard = wnd->PrevMouse = NULL;
  71.         InitWindowColors(wnd);
  72.         SendMessage(wnd, CREATE_WINDOW, 0, 0);
  73.         if (isVisible(wnd))
  74.             SendMessage(wnd, SHOW_WINDOW, 0, 0);
  75.     }
  76.     return wnd;
  77. }
  78.  
  79. /* -------- add a title to a window --------- */
  80. void AddTitle(WINDOW wnd, char *ttl)
  81. {
  82.     InsertTitle(wnd, ttl);
  83.     SendMessage(wnd, BORDER, 0, 0);
  84. }
  85.  
  86. /* ----- insert a title into a window ---------- */
  87. void InsertTitle(WINDOW wnd, char *ttl)
  88. {
  89.     if ((wnd->title=realloc(wnd->title,strlen(ttl)+1)) != NULL)
  90.         strcpy(wnd->title, ttl);
  91. }
  92.  
  93. static unsigned char line[300];
  94.  
  95. /* ------ write a line to video window client area ------ */
  96. void writeline(WINDOW wnd, char *str, int x, int y, int pad)
  97. {
  98.     char *cp;
  99.     int len;
  100.     int dif;
  101.     char *wline = calloc(1, 200);
  102.  
  103.     if (wline != NULL)    {
  104.         len = LineLength(str);
  105.         dif = strlen(str) - len;
  106.         strncpy(wline, str, ClientWidth(wnd) + dif);
  107.         if (pad)    {
  108.             cp = wline+strlen(wline);
  109.             while (len++ < ClientWidth(wnd)-x)
  110.                 *cp++ = ' ';
  111.         }
  112.         wputs(wnd, wline, x, y);
  113.         free(wline);
  114.     }
  115. }
  116.  
  117. RECT AdjustRectangle(WINDOW wnd, RECT rc)
  118. {
  119.     /* -------- adjust the rectangle ------- */
  120.     if (TestAttribute(wnd, HASBORDER))    {
  121.         if (RectLeft(rc) == 0)
  122.             --rc.rt;
  123.         else if (RectLeft(rc) < RectRight(rc) &&
  124.                 RectLeft(rc) < WindowWidth(wnd)+1)
  125.             --rc.lf;
  126.     }
  127.     if (TestAttribute(wnd, HASBORDER | HASTITLEBAR))    {
  128.         if (RectTop(rc) == 0)
  129.             --rc.bt;
  130.         else if (RectTop(rc) < RectBottom(rc) &&
  131.                 RectTop(rc) < WindowHeight(wnd)+1)
  132.             --rc.tp;
  133.     }
  134.     RectRight(rc) = max(RectLeft(rc),
  135.                         min(RectRight(rc),WindowWidth(wnd)));
  136.     RectBottom(rc) = max(RectTop(rc),
  137.                         min(RectBottom(rc),WindowHeight(wnd)));
  138.     return rc;
  139. }
  140.  
  141. /* -------- display a window's title --------- */
  142. void DisplayTitle(WINDOW wnd, RECT *rcc)
  143. {
  144.     if (GetTitle(wnd) != NULL)    {
  145.         int tlen = min(strlen(GetTitle(wnd)), WindowWidth(wnd)-2);
  146.         int tend = WindowWidth(wnd)-3-BorderAdj(wnd);
  147.         RECT rc;
  148.  
  149.         if (rcc == NULL)
  150.             rc = RelativeWindowRect(wnd, WindowRect(wnd));
  151.         else
  152.             rc = *rcc;
  153.         rc = AdjustRectangle(wnd, rc);
  154.  
  155.         if (SendMessage(wnd, TITLE, (PARAM) rcc, 0))    {
  156.             if (wnd == inFocus)    {
  157.                 foreground = cfg.clr[TITLEBAR] [HILITE_COLOR] [FG];
  158.                 background = cfg.clr[TITLEBAR] [HILITE_COLOR] [BG];
  159.             }
  160.             else    {
  161.                 foreground = cfg.clr[TITLEBAR] [STD_COLOR] [FG];
  162.                 background = cfg.clr[TITLEBAR] [STD_COLOR] [BG];
  163.             }
  164.             memset(line,' ',WindowWidth(wnd));
  165.             if (wnd->condition != ISMINIMIZED)
  166.                 strncpy(line + ((WindowWidth(wnd)-2 - tlen) / 2),
  167.                     wnd->title, tlen);
  168.             if (TestAttribute(wnd, CONTROLBOX))
  169.                 line[2-BorderAdj(wnd)] = CONTROLBOXCHAR;
  170.             if (TestAttribute(wnd, MINMAXBOX))    {
  171.                 switch (wnd->condition)    {
  172.                     case ISRESTORED:
  173.                         line[tend+1] = MAXPOINTER;
  174.                         line[tend]   = MINPOINTER;
  175.                         break;
  176.                     case ISMINIMIZED:
  177.                         line[tend+1] = MAXPOINTER;
  178.                         break;
  179.                     case ISMAXIMIZED:
  180.                         line[tend]   = MINPOINTER;
  181.                         line[tend+1] = RESTOREPOINTER;
  182.                         break;
  183.                     default:
  184.                         break;
  185.                 }
  186.             }
  187.             line[RectRight(rc)+1] = line[tend+3] = '\0';
  188.             writeline(wnd, line+RectLeft(rc),
  189.                            RectLeft(rc)+BorderAdj(wnd),
  190.                            0,
  191.                            FALSE);
  192.         }
  193.     }
  194. }
  195.  
  196. /* --- display right border shadow character of a window --- */
  197. static void near shadow_char(WINDOW wnd, int y)
  198. {
  199.     int fg = foreground;
  200.     int bg = background;
  201.     int x = WindowWidth(wnd);
  202.     int c = videochar(GetLeft(wnd)+x, GetTop(wnd)+y);
  203.  
  204.     if (TestAttribute(wnd, SHADOW) == 0)
  205.         return;
  206.     foreground = DARKGRAY;
  207.     background = BLACK;
  208.     wputch(wnd, c, x, y);
  209.     foreground = fg;
  210.     background = bg;
  211. }
  212.  
  213. /* --- display the bottom border shadow line for a window -- */
  214. static void near shadowline(WINDOW wnd, RECT rc)
  215. {
  216.     int i;
  217.     int y = GetBottom(wnd)+1;
  218.     int fg = foreground;
  219.     int bg = background;
  220.  
  221.     if ((TestAttribute(wnd, SHADOW)) == 0)
  222.         return;
  223.     for (i = 0; i < WindowWidth(wnd)+1; i++)
  224.         line[i] = videochar(GetLeft(wnd)+i, y);
  225.     line[i] = '\0';
  226.     foreground = DARKGRAY;
  227.     background = BLACK;
  228.     line[RectRight(rc)+1] = '\0';
  229.     if (RectLeft(rc) == 0)
  230.         rc.lf++;
  231.     wputs(wnd, line+RectLeft(rc), RectLeft(rc),
  232.         WindowHeight(wnd));
  233.     foreground = fg;
  234.     background = bg;
  235. }
  236.  
  237. /* ------- display a window's border ----- */
  238. void RepaintBorder(WINDOW wnd, RECT *rcc)
  239. {
  240.     int y;
  241.     unsigned int lin, side, ne, nw, se, sw;
  242.     RECT rc, clrc;
  243.  
  244.     if (!TestAttribute(wnd, HASBORDER))
  245.         return;
  246.     if (rcc == NULL)    {
  247.         rc = RelativeWindowRect(wnd, WindowRect(wnd));
  248.         if (TestAttribute(wnd, SHADOW))    {
  249.             rc.rt++;
  250.             rc.bt++;
  251.         }
  252.     }
  253.     else
  254.         rc = *rcc;
  255.     clrc = AdjustRectangle(wnd, rc);
  256.  
  257.     if (wnd == inFocus)    {
  258.         lin  = FOCUS_LINE;
  259.         side = FOCUS_SIDE;
  260.         ne   = FOCUS_NE;
  261.         nw   = FOCUS_NW;
  262.         se   = FOCUS_SE;
  263.         sw   = FOCUS_SW;
  264.     }
  265.     else    {
  266.         lin  = LINE;
  267.         side = SIDE;
  268.         ne   = NE;
  269.         nw   = NW;
  270.         se   = SE;
  271.         sw   = SW;
  272.     }
  273.     line[WindowWidth(wnd)] = '\0';
  274.     /* ---------- window title ------------ */
  275.     if (TestAttribute(wnd, HASTITLEBAR))
  276.         if (RectTop(rc) == 0)
  277.             if (RectLeft(rc) < WindowWidth(wnd)-BorderAdj(wnd))
  278.                 DisplayTitle(wnd, &rc);
  279.     foreground = FrameForeground(wnd);
  280.     background = FrameBackground(wnd);
  281.     /* -------- top frame corners --------- */
  282.     if (RectTop(rc) == 0)    {
  283.         if (RectLeft(rc) == 0)
  284.             wputch(wnd, nw, 0, 0);
  285.         if (RectLeft(rc) < WindowWidth(wnd))    {
  286.             if (RectRight(rc) >= WindowWidth(wnd)-1)
  287.                 wputch(wnd, ne, WindowWidth(wnd)-1, 0);
  288.             TopLine(wnd, lin, clrc);
  289.         }
  290.     }
  291.  
  292.     /* ----------- window body ------------ */
  293.     for (y = RectTop(rc); y <= RectBottom(rc); y++)    {
  294.         int ch;
  295.         if (y == 0 || y >= WindowHeight(wnd)-1)
  296.             continue;
  297.         if (RectLeft(rc) == 0)
  298.             wputch(wnd, side, 0, y);
  299.         if (RectLeft(rc) < WindowWidth(wnd) &&
  300.                 RectRight(rc) >= WindowWidth(wnd)-1)    {
  301.             if (TestAttribute(wnd, VSCROLLBAR))
  302.                 ch = (    y == 1 ? UPSCROLLBOX      :
  303.                           y == WindowHeight(wnd)-2  ?
  304.                                 DOWNSCROLLBOX       :
  305.                           y-1 == wnd->VScrollBox    ?
  306.                                 SCROLLBOXCHAR       :
  307.                           SCROLLBARCHAR );
  308.             else
  309.                 ch = side;
  310.             wputch(wnd, ch, WindowWidth(wnd)-1, y);
  311.         }
  312.         if (RectRight(rc) == WindowWidth(wnd))
  313.             shadow_char(wnd, y);
  314.     }
  315.  
  316.     if (RectTop(rc) <= WindowHeight(wnd)-1 &&
  317.             RectBottom(rc) >= WindowHeight(wnd)-1)    {
  318.         /* -------- bottom frame corners ---------- */
  319.         if (RectLeft(rc) == 0)
  320.             wputch(wnd, sw, 0, WindowHeight(wnd)-1);
  321.         if (RectLeft(rc) < WindowWidth(wnd) &&
  322.                 RectRight(rc) >= WindowWidth(wnd)-1)
  323.             wputch(wnd, se, WindowWidth(wnd)-1,
  324.                 WindowHeight(wnd)-1);
  325.  
  326.  
  327.         if (wnd->StatusBar == NULL)    {
  328.             /* ----------- bottom line ------------- */
  329.             memset(line,lin,WindowWidth(wnd)-1);
  330.             if (TestAttribute(wnd, HSCROLLBAR))    {
  331.                 line[0] = LEFTSCROLLBOX;
  332.                 line[WindowWidth(wnd)-3] = RIGHTSCROLLBOX;
  333.                 memset(line+1, SCROLLBARCHAR, WindowWidth(wnd)-4);
  334.                 line[wnd->HScrollBox] = SCROLLBOXCHAR;
  335.             }
  336.             line[WindowWidth(wnd)-2] = line[RectRight(rc)] = '\0';
  337.             if (RectLeft(rc) != RectRight(rc) ||
  338.             (RectLeft(rc) && RectLeft(rc) < WindowWidth(wnd)-1))
  339.                 writeline(wnd,
  340.                     line+(RectLeft(clrc)),
  341.                     RectLeft(clrc)+1,
  342.                     WindowHeight(wnd)-1,
  343.                     FALSE);
  344.         }
  345.         if (RectRight(rc) == WindowWidth(wnd))
  346.             shadow_char(wnd, WindowHeight(wnd)-1);
  347.     }
  348.     if (RectBottom(rc) == WindowHeight(wnd))
  349.         /* ---------- bottom shadow ------------- */
  350.         shadowline(wnd, rc);
  351. }
  352.  
  353. static void TopLine(WINDOW wnd, int lin, RECT rc)
  354. {
  355.     if (TestAttribute(wnd, HASMENUBAR))
  356.         return;
  357.     if (TestAttribute(wnd, HASTITLEBAR) && GetTitle(wnd))
  358.         return;
  359.     if (RectLeft(rc) == 0)    {
  360.         RectLeft(rc) += BorderAdj(wnd);
  361.         RectRight(rc) += BorderAdj(wnd);
  362.     }
  363.     if (RectRight(rc) < WindowWidth(wnd)-1)
  364.         RectRight(rc)++;
  365.  
  366.     if (RectLeft(rc) < RectRight(rc))    {
  367.         /* ----------- top line ------------- */
  368.         memset(line,lin,WindowWidth(wnd)-1);
  369.         if (TestAttribute(wnd, CONTROLBOX))    {
  370.             strncpy(line+1, "   ", 3);
  371.             *(line+2) = CONTROLBOXCHAR;
  372.         }
  373.         line[RectRight(rc)] = '\0';
  374.         writeline(wnd, line+RectLeft(rc),
  375.             RectLeft(rc), 0, FALSE);
  376.     }
  377. }
  378.  
  379. /* ------ clear the data space of a window -------- */
  380. void ClearWindow(WINDOW wnd, RECT *rcc, int clrchar)
  381. {
  382.     if (isVisible(wnd))    {
  383.         int y;
  384.         RECT rc;
  385.  
  386.         if (rcc == NULL)
  387.             rc = RelativeWindowRect(wnd, WindowRect(wnd));
  388.         else
  389.             rc = *rcc;
  390.  
  391.         if (RectLeft(rc) == 0)
  392.             RectLeft(rc) = BorderAdj(wnd);
  393.         if (RectRight(rc) > WindowWidth(wnd)-1)
  394.             RectRight(rc) = WindowWidth(wnd)-1;
  395.         SetStandardColor(wnd);
  396.         memset(line, clrchar, sizeof line);
  397.         line[RectRight(rc)+1] = '\0';
  398.         for (y = RectTop(rc); y <= RectBottom(rc); y++)    {
  399.             if (y < TopBorderAdj(wnd) ||
  400.                     y > ClientHeight(wnd)+
  401.                         (TestAttribute(wnd, HASMENUBAR) ? 1 : 0))
  402.                 continue;
  403.             writeline(wnd,
  404.                 line+(RectLeft(rc)),
  405.                 RectLeft(rc),
  406.                 y,
  407.                 FALSE);
  408.         }
  409.     }
  410. }
  411.  
  412. /* -- adjust a window's rectangle to clip it to its parent - */
  413. static RECT near ClipRect(WINDOW wnd)
  414. {
  415.     RECT rc;
  416.     rc = wnd->rc;
  417.     if (TestAttribute(wnd, SHADOW))    {
  418.         RectBottom(rc)++;
  419.         RectRight(rc)++;
  420.     }
  421.     return ClipRectangle(wnd, rc);
  422. }
  423.  
  424. /* -- get the video memory that is to be used by a window -- */
  425. void GetVideoBuffer(WINDOW wnd)
  426. {
  427.     RECT rc;
  428.     int ht;
  429.     int wd;
  430.  
  431.     rc = ClipRect(wnd);
  432.     ht = RectBottom(rc) - RectTop(rc) + 1;
  433.     wd = RectRight(rc) - RectLeft(rc) + 1;
  434.     wnd->videosave = realloc(wnd->videosave, (ht * wd * 2));
  435.     get_videomode();
  436.     if (wnd->videosave != NULL)
  437.         getvideo(rc, wnd->videosave);
  438. }
  439.  
  440. /* --- restore the video memory that was used by a window -- */
  441. void RestoreVideoBuffer(WINDOW wnd)
  442. {
  443.     if (wnd->videosave != NULL)    {
  444.         RECT rc;
  445.         rc = ClipRect(wnd);
  446.         storevideo(rc, wnd->videosave);
  447.         free(wnd->videosave);
  448.         wnd->videosave = NULL;
  449.     }
  450. }
  451.  
  452. /* ------ compute the logical line length of a window ------ */
  453. int LineLength(char *ln)
  454. {
  455.     int len = strlen(ln);
  456.     char *cp = ln;
  457.     while ((cp = strchr(cp, CHANGECOLOR)) != NULL)    {
  458.         cp++;
  459.         len -= 3;
  460.     }
  461.     cp = ln;
  462.     while ((cp = strchr(cp, RESETCOLOR)) != NULL)    {
  463.         cp++;
  464.         --len;
  465.     }
  466.     return len;
  467. }
  468.  
  469. void InitWindowColors(WINDOW wnd)
  470. {
  471.     int fbg,col;
  472.     /* ---------- set the colors ---------- */
  473.     for (fbg = 0; fbg < 2; fbg++)
  474.         for (col = 0; col < 4; col++)
  475.             wnd->WindowColors[col][fbg] = cfg.clr[GetClass(wnd)][col][fbg];
  476. }
  477.  
  478. void PutWindowChar(WINDOW wnd, int c, int x, int y)
  479. {
  480.     if (x < ClientWidth(wnd) && y < ClientHeight(wnd))
  481.         wputch(wnd, c, x+BorderAdj(wnd), y+TopBorderAdj(wnd));
  482. }
  483.  
  484. void PutWindowLine(WINDOW wnd, void *s, int x, int y)
  485. {
  486.     if (x < ClientWidth(wnd) && y < ClientHeight(wnd))    {
  487.         strncpy(line, s, 299);
  488.         line[ClientWidth(wnd)-x] = '\0';
  489.         wputs(wnd, line, x+BorderAdj(wnd), y+TopBorderAdj(wnd));
  490.     }
  491. }
  492.  
  493.